package com.lepu.app.fun.bbs.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.lepu.app.fun.bbs.BbsDetailActivity;
import com.lepu.app.fun.bbs.bean.BeanBbs;
import com.lepu.app.fun.my.MyTieZiActivity;
import com.lepu.app.main.activity.AppImageBroswerActivity;
import com.lepu.pasm.R;

public class AdapterBbs extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	public ArrayList<BeanBbs> mArrayList = null;
	public int mBbsType = 1; // 1.热帖2.康复专区3.养生天地 4 个人主页 5 我的帖子6搜索帖子
	private boolean mIsCollect = false;

	public AdapterBbs(Context context, ArrayList<BeanBbs> arrayList,
			int topicType, boolean isCollect) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mBbsType = topicType;
		mIsCollect = isCollect;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "InflateParams", "SimpleDateFormat" })
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			holder = new ViewHolder();

			if (mIsCollect) {
				convertView = mLayoutInflater.inflate(
						R.layout.my_tiezi_activity_collect_listitem, null);

				LinearLayout mainItemLayout = (LinearLayout) convertView
						.findViewById(R.id.mainItemLayout);

				TextView titleTextView = (TextView) convertView
						.findViewById(R.id.titleTextView);
				TextView descTextView = (TextView) convertView
						.findViewById(R.id.descTextView);

				holder.mainItemLayout = mainItemLayout;
				holder.titleTextView = titleTextView;
				holder.descTextView = descTextView;
			} else {
				convertView = mLayoutInflater.inflate(R.layout.bbs_list_item,
						null);

				LinearLayout mainItemLayout = (LinearLayout) convertView
						.findViewById(R.id.mainItemLayout);
				View marginTopView = (View) convertView
						.findViewById(R.id.marginTopView);
				CircularImageView iconImageView = (CircularImageView) convertView
						.findViewById(R.id.iconImageView);

				TextView userNameTextView = (TextView) convertView
						.findViewById(R.id.topic_item_name);
				TextView timeTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_time);
				TextView commentCountTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_comment);
				TextView lookCountTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_look);
				TextView titleTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_title);
				TextView contentTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_content);
				ImageView goodImageView = (ImageView) convertView
						.findViewById(R.id.bbs_item_good_icon);
				ImageView commentImage11 = (ImageView) convertView
						.findViewById(R.id.comment_image_11);
				ImageView commentImage21 = (ImageView) convertView
						.findViewById(R.id.comment_image_21);
				ImageView commentImage22 = (ImageView) convertView
						.findViewById(R.id.comment_image_22);
				ImageView commentImage31 = (ImageView) convertView
						.findViewById(R.id.comment_image_31);
				ImageView commentImage32 = (ImageView) convertView
						.findViewById(R.id.comment_image_32);
				ImageView commentImage33 = (ImageView) convertView
						.findViewById(R.id.comment_image_33);
				LinearLayout commentImageLayout = (LinearLayout) convertView
						.findViewById(R.id.comment_image_layout);
				LinearLayout commentImageLayout1 = (LinearLayout) convertView
						.findViewById(R.id.comment_image_layout1);
				LinearLayout commentImageLayout2 = (LinearLayout) convertView
						.findViewById(R.id.comment_image_layout2);
				LinearLayout commentImageLayout3 = (LinearLayout) convertView
						.findViewById(R.id.comment_image_layout3);

				RelativeLayout headMyStateRelativeLayout = (RelativeLayout) convertView
						.findViewById(R.id.headMyStateRelativeLayout);
				RelativeLayout headIconRelativeLayout = (RelativeLayout) convertView
						.findViewById(R.id.headIconRelativeLayout);
				TextView topic_item_state = (TextView) convertView
						.findViewById(R.id.topic_item_state);
				TextView topic_item_forum = (TextView) convertView
						.findViewById(R.id.topic_item_forum);

				LinearLayout topItemLayout = (LinearLayout) convertView
						.findViewById(R.id.topItemLayout);
				ImageView topGoodImageView = (ImageView) convertView
						.findViewById(R.id.bbs_item_top_good_icon);
				TextView topTitleTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_top_title);
				TextView topTimeTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_top_time);
				TextView topLookCountTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_top_look);
				TextView topCommentCountTextView = (TextView) convertView
						.findViewById(R.id.bbs_item_top_comment);
				TextView topForumTextView = (TextView) convertView
						.findViewById(R.id.topic_item_top_forum);

				holder.mainItemLayout = mainItemLayout;
				holder.marginTopView = marginTopView;
				holder.iconImageView = iconImageView;
				holder.userNameTextView = userNameTextView;
				holder.timeTextView = timeTextView;
				holder.commentCountTextView = commentCountTextView;
				holder.lookCountTextView = lookCountTextView;
				holder.titleTextView = titleTextView;
				holder.contentTextView = contentTextView;
				holder.goodImageView = goodImageView;

				holder.commentImage11 = commentImage11;
				holder.commentImage21 = commentImage21;
				holder.commentImage22 = commentImage22;
				holder.commentImage31 = commentImage31;
				holder.commentImage32 = commentImage32;
				holder.commentImage33 = commentImage33;
				holder.commentImageLayout = commentImageLayout;
				holder.commentImageLayout1 = commentImageLayout1;
				holder.commentImageLayout2 = commentImageLayout2;
				holder.commentImageLayout3 = commentImageLayout3;

				holder.headMyStateRelativeLayout = headMyStateRelativeLayout;
				holder.headIconRelativeLayout = headIconRelativeLayout;
				holder.topic_item_state = topic_item_state;
				holder.topic_item_forum = topic_item_forum;

				// top
				holder.topItemLayout = topItemLayout;
				holder.topGoodImageView = topGoodImageView;
				holder.topTitleTextView = topTitleTextView;
				holder.topTimeTextView = topTimeTextView;
				holder.topLookCountTextView = topLookCountTextView;
				holder.topCommentCountTextView = topCommentCountTextView;
				holder.topForumTextView = topForumTextView;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.mainItemLayout.setTag(position);

		final BeanBbs item = mArrayList.get(position);

		if (mIsCollect) {

			holder.titleTextView.setText(item.Title);
			holder.descTextView.setText(item.Abstract);

			// item监听
			holder.mainItemLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					MyTieZiActivity instance = (MyTieZiActivity) AppManager
							.getAppManager().getActivity(MyTieZiActivity.class);
					if (instance != null) {
						instance.mCurrentBeanBbs = item;
						instance.mCurrentOpePosition = position;

						Intent intent = new Intent(mContext,
								BbsDetailActivity.class);
						intent.putExtra("threadId", item.TargetID);
						instance.startActivity(intent, true);
					}
				}
			});
		} else {
			// 置顶
			if (item.bbsIsTop == 1 && mBbsType != 1) { // 热帖不显示置顶
				holder.topItemLayout.setVisibility(View.VISIBLE);
				holder.mainItemLayout.setVisibility(View.GONE);

				// 精华
				if (item.bbsIsGood == 1) {
					holder.topGoodImageView.setVisibility(View.VISIBLE);
					holder.topTitleTextView.setText("                "
							+ item.bbsTitle);
				} else {
					holder.topGoodImageView.setVisibility(View.GONE);
					holder.topTitleTextView
							.setText("         " + item.bbsTitle);
				}

				// time
				long timeLong = Long.valueOf(item.bbsTime) * 1000;
				SimpleDateFormat sdf = new SimpleDateFormat(
						DateUtilBase.DATE_ALL_ALL);

				String timeString = sdf.format(new Date(timeLong));

				holder.topTimeTextView.setText(DateUtilBase
						.friendly_time(timeString));

				holder.topCommentCountTextView.setText(" "
						+ item.bbsCommentCount);
				holder.topLookCountTextView.setText(" " + item.bbsLookCount);

				holder.topForumTextView.setSelected(true);

				if (mBbsType == 2 || mBbsType == 3) {
					holder.topForumTextView.setVisibility(View.GONE);
				} else {
					holder.topForumTextView.setVisibility(View.VISIBLE);
				}

				if (mBbsType == 1) {
					// 板块id 1-支架人生 2-支架术后
					int forumID = UtilityBase.parseInt(item.ForumID);

					if (forumID == 1) {
						holder.topForumTextView.setText(mContext.getResources()
								.getString(R.string.bbs_type1));
						holder.topForumTextView.setTextColor(mContext
								.getResources().getColor(
										R.color.color_my_medicion));
						holder.topForumTextView
								.setBackgroundResource(R.drawable.app_text_selector2);
					} else if (forumID == 2) {
						holder.topForumTextView.setText(mContext.getResources()
								.getString(R.string.bbs_type2));
						holder.topForumTextView
								.setTextColor(mContext.getResources().getColor(
										R.color.color_my_food));
						holder.topForumTextView
								.setBackgroundResource(R.drawable.app_text_selector3);
					}
				}

				// item监听
				holder.topItemLayout
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								Intent intent = new Intent(mContext,
										BbsDetailActivity.class);
								intent.putExtra("threadId", item.bbsID);
								((BaseFragmentActivity) mContext)
										.startActivity(intent, true);
							}
						});

				// 正常
			} else {
				holder.topItemLayout.setVisibility(View.GONE);
				holder.mainItemLayout.setVisibility(View.VISIBLE);
				// topView
				if (mArrayList.size() > 1 && position > 0
						&& mArrayList.get(position - 1).bbsIsTop == 1
						&& mBbsType != 1) {
					holder.marginTopView.setVisibility(View.VISIBLE);
				} else {
					holder.marginTopView.setVisibility(View.GONE);
				}

				// 头像
				LoginUtil.loadSelfUserImage(mContext,holder.iconImageView,item.bbsUserId);

				if (item.bbsUserName != null) {
					if (item.bbsUserName.length() > 10) {
						String name = item.bbsUserName.substring(0, 10) + "...";
						holder.userNameTextView.setText(name);
					} else {
						holder.userNameTextView.setText(item.bbsUserName);
					}
				}

				holder.commentCountTextView.setText(" " + item.bbsCommentCount);
				holder.lookCountTextView.setText(" " + item.bbsLookCount);
				holder.contentTextView.setText(item.bbsContent);
				UIHelper.setTextViewLines(holder.contentTextView, 2);

				// 精华
				if (item.bbsIsGood == 1) {
					holder.goodImageView.setVisibility(View.VISIBLE);
					holder.titleTextView.setText("     " + item.bbsTitle);
				} else {
					holder.goodImageView.setVisibility(View.GONE);
					holder.titleTextView.setText(item.bbsTitle);
				}
				UIHelper.setTextViewLines(holder.titleTextView, 2);

				// time
				long timeLong = Long.valueOf(item.bbsReplyTime) * 1000;
				SimpleDateFormat sdf = new SimpleDateFormat(
						DateUtilBase.DATE_ALL_ALL);

				String timeString = sdf.format(new Date(timeLong));

				holder.timeTextView.setText(DateUtilBase
						.friendly_time(timeString));

				// 图片
				if (item.bbsImageUrl != null && item.bbsImageUrl.size() > 0) {
					boolean hasDate = false;
					for (int i = 0; i < item.bbsImageUrl.size(); i++) {
						if (!TextUtils.isEmpty(item.bbsImageUrl.get(i))) {
							hasDate = true;
						}
					}

					if (hasDate) {
						holder.commentImageLayout.setVisibility(View.VISIBLE);
					} else {
						holder.commentImageLayout.setVisibility(View.GONE);
					}

					if (item.bbsImageUrl.size() == 1) {
						holder.commentImageLayout1.setVisibility(View.VISIBLE);
						holder.commentImageLayout2.setVisibility(View.GONE);
						holder.commentImageLayout3.setVisibility(View.GONE);
						UniversalImageLoadTool.disPlay(item.bbsImageUrl.get(0),holder.commentImage11,R.drawable.app_icon_load_image);
						holder.commentImage11.setOnClickListener(imageClickListener((String[]) item.bbsImageUrl.toArray(new String[item.bbsImageUrl.size()]), 1));
					} else if (item.bbsImageUrl.size() == 2) {
						holder.commentImageLayout2.setVisibility(View.VISIBLE);
						holder.commentImageLayout1.setVisibility(View.GONE);
						holder.commentImageLayout3.setVisibility(View.GONE);
						UniversalImageLoadTool.disPlay(item.bbsImageUrl.get(0),holder.commentImage21,R.drawable.app_icon_load_image);
						UniversalImageLoadTool.disPlay(item.bbsImageUrl.get(1),holder.commentImage22,R.drawable.app_icon_load_image);
						holder.commentImage21.setOnClickListener(imageClickListener((String[]) item.bbsImageUrl.toArray(new String[item.bbsImageUrl.size()]), 1));
						holder.commentImage22.setOnClickListener(imageClickListener((String[]) item.bbsImageUrl.toArray(new String[item.bbsImageUrl.size()]), 2));
					} else if (item.bbsImageUrl.size() == 3) {
						holder.commentImageLayout3.setVisibility(View.VISIBLE);
						holder.commentImageLayout1.setVisibility(View.GONE);
						holder.commentImageLayout2.setVisibility(View.GONE);
						UniversalImageLoadTool.disPlay(item.bbsImageUrl.get(0),holder.commentImage31,R.drawable.app_icon_load_image);
						UniversalImageLoadTool.disPlay(item.bbsImageUrl.get(1),holder.commentImage32,R.drawable.app_icon_load_image);
						UniversalImageLoadTool.disPlay(item.bbsImageUrl.get(2),holder.commentImage33,R.drawable.app_icon_load_image);
						holder.commentImage31.setOnClickListener(imageClickListener((String[]) item.bbsImageUrl.toArray(new String[item.bbsImageUrl.size()]), 1));
						holder.commentImage32.setOnClickListener(imageClickListener((String[]) item.bbsImageUrl.toArray(new String[item.bbsImageUrl.size()]), 2));
						holder.commentImage33.setOnClickListener(imageClickListener((String[]) item.bbsImageUrl.toArray(new String[item.bbsImageUrl.size()]), 3));
					} else {

					}
				} else {
					holder.commentImageLayout.setVisibility(View.GONE);
				}

				// 头像监听
				holder.iconImageView
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								LoginUtil.jumpUserInfo(
										(BaseFragmentActivity) mContext,
										item.bbsUserId);
							}
						});

				holder.topic_item_forum.setSelected(true);

				if (mBbsType == 2 || mBbsType == 3) {
					holder.topic_item_forum.setVisibility(View.GONE);
				} else {
					holder.topic_item_forum.setVisibility(View.VISIBLE);
				}

				if (mBbsType == 1) {
					// 板块id 1-支架人生 2-支架术后
					int forumID = UtilityBase.parseInt(item.ForumID);

					if (forumID == 1) {
						holder.topic_item_forum.setText(mContext.getResources()
								.getString(R.string.bbs_type1));
						holder.topic_item_forum.setTextColor(mContext
								.getResources().getColor(
										R.color.color_my_medicion));
						holder.topic_item_forum
								.setBackgroundResource(R.drawable.app_text_selector2);
					} else if (forumID == 2) {
						holder.topic_item_forum.setText(mContext.getResources()
								.getString(R.string.bbs_type2));
						holder.topic_item_forum
								.setTextColor(mContext.getResources().getColor(
										R.color.color_my_food));
						holder.topic_item_forum
								.setBackgroundResource(R.drawable.app_text_selector3);
					}
				}

				if (mBbsType == 4 || mBbsType == 5) {
					holder.headIconRelativeLayout.setVisibility(View.VISIBLE);

					int forumID = UtilityBase.parseInt(item.ForumID);

					if (forumID == 1) {
						holder.topic_item_forum.setText(mContext.getResources()
								.getString(R.string.bbs_type1));
						holder.topic_item_forum.setTextColor(mContext
								.getResources().getColor(
										R.color.color_my_medicion));
						holder.topic_item_forum
								.setBackgroundResource(R.drawable.app_text_selector2);
					} else if (forumID == 2) {
						holder.topic_item_forum.setText(mContext.getResources()
								.getString(R.string.bbs_type2));
						holder.topic_item_forum
								.setTextColor(mContext.getResources().getColor(
										R.color.color_my_food));
						holder.topic_item_forum
								.setBackgroundResource(R.drawable.app_text_selector3);
					} else {
						holder.topic_item_forum.setText("");
					}

					if (mBbsType == 4) {
						// 个人主页
						holder.topic_item_state.setText("");
						holder.iconImageView.setVisibility(View.INVISIBLE);
						holder.userNameTextView.setVisibility(View.INVISIBLE);
					} else {
						// 我的帖子 审核状态 0-拒绝 1-通过
						if (!TextUtils.isEmpty(item.CheckStatus)
								&& item.CheckStatus.equals("1")) {
							holder.topic_item_state.setText("");
							holder.headMyStateRelativeLayout
									.setVisibility(View.GONE);
						} else {
							holder.topic_item_state.setText("拒绝");
							holder.headMyStateRelativeLayout
									.setVisibility(View.VISIBLE);
						}
					}
				} else {
					holder.headIconRelativeLayout.setVisibility(View.VISIBLE);
					holder.headMyStateRelativeLayout.setVisibility(View.GONE);
				}

				holder.commentCountTextView.setVisibility(View.VISIBLE);
				holder.lookCountTextView.setVisibility(View.VISIBLE);

				// item监听
				holder.mainItemLayout
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								Intent intent = new Intent(mContext,
										BbsDetailActivity.class);
								intent.putExtra("threadId", item.bbsID);
								((BaseFragmentActivity) mContext)
										.startActivity(intent, true);
							}
						});
			}
		}

		// 长按监听
		holder.mainItemLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// 必须空实现，否则外部长按监听不起作用
				return false;
			}
		});
		return convertView;
	}

	// 图片监听
	private View.OnClickListener imageClickListener(final String[] urlArray,
			final int position) {
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,
						AppImageBroswerActivity.class);
				intent.putExtra("isShowTopbar", false);
				intent.putExtra("index", position - 1);
				intent.putExtra("urlArray", urlArray);
				((BaseFragmentActivity) mContext).startActivity(intent);
			}
		};
	}

	private static class ViewHolder {

		LinearLayout mainItemLayout;

		View marginTopView;

		CircularImageView iconImageView;

		TextView userNameTextView;
		TextView timeTextView;
		TextView commentCountTextView;
		TextView lookCountTextView;

		TextView titleTextView;
		TextView contentTextView;

		ImageView goodImageView;

		ImageView commentImage11, commentImage21, commentImage22,
				commentImage31, commentImage32, commentImage33;
		LinearLayout commentImageLayout, commentImageLayout1,
				commentImageLayout2, commentImageLayout3;

		// my new add wuxd
		RelativeLayout headMyStateRelativeLayout;
		RelativeLayout headIconRelativeLayout;
		TextView topic_item_state;
		TextView topic_item_forum;

		// collect
		TextView descTextView;

		// top
		LinearLayout topItemLayout;
		ImageView topGoodImageView;
		TextView topTitleTextView;
		TextView topTimeTextView;
		TextView topLookCountTextView;
		TextView topCommentCountTextView;
		TextView topForumTextView;

	}
}
